package com.whsxt.common.util;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/**
 * 获取web容器
 * 
 * @author mrt
 *
 */
public class WebContextUtil {

	

	/**
	 * 低层源码类 获取request请求
	 * @return
	 */
	public static HttpServletRequest getRequest() {
		ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
		return servletRequestAttributes.getRequest();
	}
	/**
	 * 根据request 获取session
	 * @return
	 */
	public static HttpSession getSession() {
		return getRequest().getSession();
	}
	/**
	 * 获取servletcontext对象  根据当前线程获取   跟request 和session无关
	 * @return
	 */
	public static ServletContext getContext() {
		WebApplicationContext currentWebApplicationContext = ContextLoader.getCurrentWebApplicationContext();
		return currentWebApplicationContext.getServletContext();
	}

}
